{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2fed764d",
   "metadata": {},
   "source": [
    "# 优化ASR（自动语音识别）机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8e0eb958",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'nSECRET_KEY = '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ac35403",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93fc5e39",
   "metadata": {},
   "outputs": [],
   "source": [
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "xu_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52b1d3d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f01efeb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "asr_output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5128430",
   "metadata": {},
   "source": [
    "# 连接图灵机器人"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "885f899e",
   "metadata": {},
   "source": [
    "*   接口地址\n",
    "> http://openapi.turingapi.com/openapi/api/v2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "656f10e1",
   "metadata": {},
   "source": [
    "*   请求方式\n",
    "> Post"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd80fb7d",
   "metadata": {},
   "source": [
    "*   请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b8a22867",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9415d586",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_KEY = \"d5d5bb66f66d4da8bedb378df7adfa70\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ac0ed5a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def TL_API(key,text):\n",
    "    TL_URL = \"http://openapi.turingapi.com/openapi/api/v2\"\n",
    "\n",
    "    payload={\n",
    "        \"reqType\":0,\n",
    "        \"perception\": {\n",
    "            \"inputText\": {\n",
    "                \"text\": text\n",
    "            },\n",
    "        },\n",
    "        \"userInfo\": {\n",
    "            \"apiKey\": TL_KEY,\n",
    "            \"userId\": \"000001\"\n",
    "        }\n",
    "    }\n",
    "    res = requests.post(TL_URL,data = json.dumps(payload))\n",
    "    return res.json()['results'][0]['values']['text']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c0f38fa",
   "metadata": {},
   "source": [
    "# 接入对话机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d90afba",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "\n",
    "TL_API(TL_KEY,asr_output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fc36a3b",
   "metadata": {},
   "source": [
    "# 语音合成测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3585ffff",
   "metadata": {},
   "outputs": [],
   "source": [
    "tts(xu_token,TEXT)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22a19b21",
   "metadata": {},
   "source": [
    "# 在jupyter中播放音频文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31c1060e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87da89ac",
   "metadata": {},
   "source": [
    "# 智能对话机器人最终版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "450f0e02",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition \n",
    "from pydub import AudioSegment\n",
    "from pydub.playback import play \n",
    "from bdasr import fetch_token,asr \n",
    "from bdtts import tts \n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "xu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_output = eval(asr(xu_token,'1.wav'))['result'][0]\n",
    "\n",
    "TL_result = TL_API(TL_KEY,asr_output)\n",
    "\n",
    "tts(xu_token,TL_result)\n",
    "\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
